<template>
  <div id="root">
    <div class="todo-container">
      <div class="tod-wrap">
        <!-- <TodoHeader :addTodo="addTodo"></TodoHeader> -->
        <!--自定义事件 子给父-->
        <TodoHeader @addTodo="addTodo"></TodoHeader>
        <!-- <TodoList
          :todos="todos"
          :changeDone="changeDone"
          :deleteTodo="deleteTodo"
        ></TodoList> -->
        <!-- <TodoList
          :todos="todos"
          :changeDone="changeDone"
          :deleteTodo="deleteTodo"
        ></TodoList> -->
        <!-- 全局消息总线 -->
        <TodoList
          :todos="todos"
        ></TodoList>
        <!--自定义事件 子给父-->
        <TodoFooter
          :todos="todos"
          @deleteFinish="deleteFinish"
          @selectAll="selectAll"
        ></TodoFooter>
      </div>
    </div>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoList from "./components/TodoList.vue";
import TodoFooter from "./components/TodoFooter.vue";
import pubsub from 'pubsub-js';
export default {
  name: "App",
  components: {
    TodoHeader,
    TodoList,
    TodoFooter,
  },
  data() {
    return {
      welcomeMsg: "欢迎学习Vue!",
      // todos: [
      //   { id: "001", title: "吃饭", done: true },
      //   { id: "002", title: "睡觉", done: false },
      //   { id: "003", title: "打豆豆", done: true },
      // ],
      todos: JSON.parse(sessionStorage.getItem("todo")) || [],
    };
  },
  mounted(){
    this.$bus.$on('changeDone',this.changeDone);
    //this.$bus.$on('deleteTodo',this.deleteTodo);
    this.$bus.$on('updateTodo',this.updateTodo);
    //消息订阅
    pubsub.subscribe('deleteTodo',(msgName, data)=>{
      this.deleteTodo(data);
    });
  },
  beforeDestroy(){
    this.$bus.$off('changeDone',this.changeDone);
    this.$bus.$off('updateTodo',this.updateTodo);
    //this.$bus.$off('deleteTodo',this.deleteTodo);
    // 取消消息订阅
    pubsub.unsubscribe('deleteTodo');
  },
  created(){
    window.addEventListener('popstate',this.handlePopState);
    console.log('添加监听');
  },
  destroyed(){
    window.removeEventListener('popstate',this.handlePopState);
    console.log('取消监听');
  },
  beforeRouteLeave (to, from, next) {
    // ...
    next(false);
  },
  methods: {
    addTodo(todoObj) {
      this.todos.unshift(todoObj);
    },
    changeDone(id) {
      this.todos.forEach((todo) => {
        if (id === todo.id) {
          todo.done = !todo.done;
        }
      });
    },
    deleteTodo(id) {
      this.todos = this.todos.filter((todo) => {
        return id !== todo.id;
      });
    },
    deleteFinish() {
      this.todos = this.todos.filter((todo) => {
        return !todo.done;
      });
    },
    selectAll(d) {
      this.todos.forEach((todo) => {
        todo.done = d;
      });
    },
    handlePopState(event){
      event.preventDefault();
      console.log('哈哈哈');
      alert('试试');
    },
    updateTodo(id,title){
      this.todos.forEach((todo) => {
        if(id == todo.id){
          todo.title = title;
        }
      });
    }
  },
  watch: {
    todos: {
      deep: true,
      handler(value) {
        sessionStorage.setItem("todo", JSON.stringify(value));
      },
    },
  },
};
</script>

<style>
/* base */
body {
  background: #fff;
}
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  margin-left: 10px;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.2);
}
.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(11, 92, 124);
}
.btn-edit:hover {
  color: #fff;
  background-color: rgb(11, 92, 124);
}
.btn:focus {
  outline: none;
}
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .tod-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>